/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月4日         luwei
 */

package com.coocaa.launcher.framework.sidebar.item;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.BaseView;

public abstract class ItemView extends BaseView {

    protected OnItemFocusChangeListener mItemFocusChangeListener = null;
    protected OnItemKeyEventListener mItemKeyEventListener = null;
    protected OnItemClickListener mItemClickListener = null;


    public interface OnItemFocusChangeListener {
        public void onItemFocusChange(View v, boolean focus);
    }

    public interface OnItemKeyEventListener {
        public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent);
    }

    public interface OnItemClickListener {
        public void onItemClick(View v);
    }

    public ItemView(Context context) {
        super(context);
        setOnHoverListener(this);
        setOnFocusChangeListener(this);
    }

    public void setItemFocusChangeListener(OnItemFocusChangeListener listener) {
        mItemFocusChangeListener = listener;
    }

    public void setItemKeyEventListener(OnItemKeyEventListener listener) {
        mItemKeyEventListener = listener;
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (mItemKeyEventListener.onItemKeyEvent(v, keyCode, event)) {
                return true;
            }
        }
        return super.onKey(v, keyCode, event);
    }

    @Override
    public void onClick(View v) {
        LogUtils.d("onclick itemview");
        mItemClickListener.onItemClick(v);
        super.onClick(v);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        mItemFocusChangeListener.onItemFocusChange(v, hasFocus);
        super.onFocusChange(v, hasFocus);
    }

    public abstract void destory();

}
